﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.MEP.Electric.Calculator;

namespace YArchitech.Revit.MEP.Calculator
{
	public class SelectionTagUtility
	{
		public SelectionTagUtility(UIApplication uiapplication)
		{
			this.uiApp = uiapplication;
			this.doc = this.uiApp.ActiveUIDocument.Document;
			this.selection = this.uiApp.ActiveUIDocument.Selection;
		}

		public void ShowMainDlg()
		{
			try
			{
				if (this.m_dlgIllCal == null)
				{
					this.m_dlgIllCal = new DlgIllCalMain(this.uiApp);
				}
				DialogResult dialogResult = this.m_dlgIllCal.ShowDialog();
				if (dialogResult != DialogResult.OK)
				{
					if (dialogResult == DialogResult.Cancel)
					{
						this.FormClosing();
					}
				}
				else
				{
					if (this.m_dlgIllCal.selectionMethod == 0)
					{
						AnnotationSymbol annotationSymbol = this.SelectRoomTag();
						if (annotationSymbol != null)
						{
							string text;
							this.AddRoomTagToDlg(annotationSymbol, out text);
							if (!string.IsNullOrEmpty(text))
							{
								TaskDialog.Show("盈建科", text + "已经存在，不能重复添加。");
							}
						}
					}
					else if (this.m_dlgIllCal.selectionMethod == 1)
					{
						List<AnnotationSymbol> list = this.SelectRoomTags();
						if (list != null)
						{
							string text2 = "";
							foreach (AnnotationSymbol tag in list)
							{
								string text3;
								this.AddRoomTagToDlg(tag, out text3);
								if (!string.IsNullOrEmpty(text3))
								{
									text2 = text2 + " " + text3;
								}
							}
							if (!string.IsNullOrEmpty(text2))
							{
								TaskDialog.Show("盈建科", text2 + "已经存在，不能重复添加。");
							}
						}
					}
					else if (this.m_dlgIllCal.selectionMethod == 2)
					{
						List<AnnotationSymbol> list2 = this.SelectAllRoomTags();
						if (list2 != null)
						{
							string text4 = "";
							foreach (AnnotationSymbol tag2 in list2)
							{
								string text5;
								this.AddRoomTagToDlg(tag2, out text5);
								if (!string.IsNullOrEmpty(text5))
								{
									text4 = text4 + " " + text5;
								}
							}
							if (!string.IsNullOrEmpty(text4))
							{
								TaskDialog.Show("盈建科", text4 + "已经存在，不能重复添加。");
							}
						}
					}
					this.ShowMainDlg();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private AnnotationSymbol SelectRoomTag()
		{
			try
			{
				SelectionRoomTagFilter selectionRoomTagFilter = new SelectionRoomTagFilter();
				Reference reference = this.selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionRoomTagFilter, "请选择一个房间照明标记");
				if (reference != null)
				{
					return this.doc.GetElement(reference) as AnnotationSymbol;
				}
			}
			catch (Exception)
			{
			}
			return null;
		}

		private List<AnnotationSymbol> SelectRoomTags()
		{
			try
			{
				List<AnnotationSymbol> list = new List<AnnotationSymbol>();
				SelectionRoomTagFilter selectionRoomTagFilter = new SelectionRoomTagFilter();
				foreach (Reference reference in this.selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, selectionRoomTagFilter, "请选择房间照明标记："))
				{
					AnnotationSymbol item = this.doc.GetElement(reference) as AnnotationSymbol;
					list.Add(item);
				}
				return list;
			}
			catch (Exception)
			{
			}
			return null;
		}

		private List<AnnotationSymbol> SelectAllRoomTags()
		{
			List<AnnotationSymbol> list = new List<AnnotationSymbol>();
			foreach (Element element in ((IEnumerable<Element>)Common.GetElementByCategory(this.doc, BuiltInCategory.OST_GenericAnnotation)))
			{
				AnnotationSymbol annotationSymbol = element as AnnotationSymbol;
				if (annotationSymbol != null && annotationSymbol.Name == "房间照明标记")
				{
					list.Add(annotationSymbol);
				}
			}
			this.CreateTabPage(list);
			return list;
		}

		private void CreateTabPage(List<AnnotationSymbol> allRoomTag)
		{
			List<Element> list = new List<Element>();
			foreach (AnnotationSymbol elem in allRoomTag)
			{
				ElementId elementId = new ElementId(Convert.ToInt32(elem.GetParameter("房间照明标记标高").AsString()));
				Element elementById = this.doc.GetElementById(elementId);
				bool flag = false;
				using (List<Element>.Enumerator enumerator2 = list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Id == elementId)
						{
							flag = true;
						}
					}
				}
				if (!flag)
				{
					list.Add(elementById);
				}
			}
			foreach (Element element in list)
			{
				Level level = (Level)element;
				if (this.m_dlgIllCal.tabLevels.TabPages[level.Name] == null)
				{
					TabPage tabPage = new TabPage();
					tabPage.Text = level.Name;
					tabPage.Name = level.Name;
					DlgIllCalData dlgIllCalData = new DlgIllCalData(this.uiApp);
					dlgIllCalData.Name = "DlgIllCalData";
					dlgIllCalData.TopLevel = false;
					tabPage.Controls.Add(dlgIllCalData);
					dlgIllCalData.Show();
					this.m_dlgIllCal.tabLevels.TabPages.Add(tabPage);
				}
			}
		}

		private void AddRoomTagToDlg(AnnotationSymbol tag, out string prompt)
		{
			ElementId elementId = new ElementId(Convert.ToInt32(tag.GetParameter("房间照明标记标高").AsString()));
			Element elementById = this.doc.GetElementById(elementId);
			prompt = "";
			if (elementById != null)
			{
				bool flag = false;
				DlgIllCalData dlgIllCalData = this.m_dlgIllCal.tabLevels.TabPages[elementById.Name].Controls["DlgIllCalData"] as DlgIllCalData;
				for (int i = 0; i < dlgIllCalData.illCalDataSet.listIllCalData.Count; i++)
				{
					if (dlgIllCalData.illCalDataSet.listIllCalData.ElementAt(i).roomData.RoomTagID != null && tag.Id == dlgIllCalData.illCalDataSet.listIllCalData.ElementAt(i).roomData.RoomTagID)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					this.AddRoomTagAndFilldgv(dlgIllCalData, tag);
					return;
				}
				prompt = string.Concat(new string[]
				{
					"\"",
					tag.GetParameter("房间名称").AsString(),
					":",
					tag.GetParameter("房间编号").AsString(),
					"\""
				});
			}
		}

		private void AddRoomTagAndFilldgv(DlgIllCalData dlgIllCalData, AnnotationSymbol tag)
		{
			this.InsertNewRow(dlgIllCalData, tag);
			RoomData roomData = dlgIllCalData.illCalDataSet.activeIllCalData.roomData;
			roomData.RoomNumber = tag.GetParameter("房间编号").AsString();
			roomData.RoomName = tag.GetParameter("房间名称").AsString();
			roomData.RoomArea = tag.GetParameter("房间面积").AsDouble();
			roomData.RoomHeight = tag.GetParameter("房间高度").AsDouble();
			roomData.WorkHeight = tag.GetParameter("工作面高度").AsDouble();
			roomData.InstallHeight = tag.GetParameter("安装高度").AsDouble();
			roomData.CeilingFactor = tag.GetParameter("顶棚反射系数").AsDouble();
			roomData.WallUpFactor = tag.GetParameter("墙上部反射系数").AsDouble();
			roomData.WallDownFactor = tag.GetParameter("墙下部反射系数").AsDouble();
			roomData.FloorFactor = tag.GetParameter("地面反射系数").AsDouble();
			roomData.Lux = tag.GetParameter("标准照度").AsDouble();
			roomData.Lpd = tag.GetParameter("标准功率密度").AsDouble();
			roomData.RoomPerimeter = tag.GetParameter("房间周长").AsDouble();
			roomData.WallPerimeter = tag.GetParameter("墙周长").AsDouble();
			roomData.WindowArea = tag.GetParameter("窗面积").AsDouble();
			roomData.RoomTagID = tag.Id;
			if (tag.GetParameter("房间计算状态").AsInteger() == 1)
			{
				roomData.CalState = RoomCalState.eCaled;
				this.GetCalInfo(dlgIllCalData, tag);
			}
		}

		private void GetCalInfo(DlgIllCalData dlgIllCalData, AnnotationSymbol tag)
		{
			LampData lampData = dlgIllCalData.illCalDataSet.activeIllCalData.lampData;
			Guid guid = new Guid(tag.GetParameter("灯具GUID").AsString());
			lampData.GUID = guid;
			lampData.Type = tag.GetParameter("灯具类型").AsString();
			lampData.Name = tag.GetParameter("灯具名称").AsString();
			lampData.Spec = tag.GetParameter("灯具规格").AsString();
			lampData.MaintenanceFactor = tag.GetParameter("维护系数").AsDouble();
			lampData.UseFactor = tag.GetParameter("利用系数").AsDouble();
			lampData.BallastPower = tag.GetParameter("镇流器").AsDouble();
			LightSourceData lightSourceData = dlgIllCalData.illCalDataSet.activeIllCalData.lightSourceData;
			lightSourceData.Type = tag.GetParameter("光源类型").AsString();
			lightSourceData.Name = tag.GetParameter("光源名称").AsString();
			lightSourceData.Model = tag.GetParameter("光源型号").AsString();
			lightSourceData.Number = tag.GetParameter("光源数量").AsInteger();
			lightSourceData.Power = tag.GetParameter("光源功率").AsDouble();
			lightSourceData.LuminousFlux = tag.GetParameter("光通量").AsDouble();
			CalResultData calResultData = dlgIllCalData.illCalDataSet.activeIllCalData.calResultData;
			calResultData.LampNumber = tag.GetParameter("灯具套数").AsInteger();
			calResultData.Lux = tag.GetParameter("照度").AsDouble();
			string a = tag.GetParameter("照度校核").AsString();
			if (a == "满足")
			{
				calResultData.LuxChecked = CheckedState.eMeet;
			}
			else if (a == "不满足")
			{
				calResultData.LuxChecked = CheckedState.eNoMeet;
			}
			calResultData.Lpd = tag.GetParameter("功率密度").AsDouble();
			calResultData.LpdEx = tag.GetParameter("换算功率密度").AsDouble();
			string a2 = tag.GetParameter("功率密度校核").AsString();
			if (a2 == "满足")
			{
				calResultData.LpdChecked = CheckedState.eMeet;
				return;
			}
			if (a2 == "不满足")
			{
				calResultData.LpdChecked = CheckedState.eNoMeet;
			}
		}

		public void InsertNewRow(DlgIllCalData dlgIllCalData, AnnotationSymbol tag)
		{
			IllCalData illCalData = new IllCalData();
			illCalData.roomData.RoomIndex = dlgIllCalData.dgvRoomData.Rows.Count + 1;
			illCalData.roomData.CalState = (RoomCalState)tag.GetParameter("房间计算状态").AsInteger();
			dlgIllCalData.illCalDataSet.listIllCalData.Add(illCalData);
			dlgIllCalData.illCalDataSet.activeIllCalData = dlgIllCalData.illCalDataSet.listIllCalData.Last<IllCalData>();
			dlgIllCalData.RefreshDataGridView();
			int index = dlgIllCalData.dgvRoomData.Rows.Count - 1;
			dlgIllCalData.dgvRoomData.CurrentCell = dlgIllCalData.dgvRoomData.Rows[index].Cells[0];
			dlgIllCalData.InitMaintenanceFactor();
			dlgIllCalData.InitLampType();
		}

		private void FormClosing()
		{
			foreach (object obj in this.m_dlgIllCal.tabLevels.TabPages)
			{
				foreach (IllCalData illCalData in (((TabPage)obj).Controls["DlgIllCalData"] as DlgIllCalData).illCalDataSet.listIllCalData)
				{
					this.WriteTag(illCalData);
				}
			}
		}

		public void WriteTag(IllCalData illCalData)
		{
			Transaction transaction = new Transaction(this.doc, "hy");
			transaction.Start();
			ElementId roomTagID = illCalData.roomData.RoomTagID;
			AnnotationSymbol elem = this.doc.GetElementById(roomTagID) as AnnotationSymbol;
			if (illCalData.roomData.CalState == RoomCalState.eCaled)
			{
				elem.GetParameter("房间计算状态").Set((int)illCalData.roomData.CalState);
				elem.GetParameter("房间高度").Set(illCalData.roomData.RoomHeight);
				elem.GetParameter("工作面高度").Set(illCalData.roomData.WorkHeight);
				elem.GetParameter("安装高度").Set(illCalData.roomData.InstallHeight);
				elem.GetParameter("顶棚反射系数").Set(illCalData.roomData.CeilingFactor);
				elem.GetParameter("墙上部反射系数").Set(illCalData.roomData.WallUpFactor);
				elem.GetParameter("墙下部反射系数").Set(illCalData.roomData.WallDownFactor);
				elem.GetParameter("地面反射系数").Set(illCalData.roomData.FloorFactor);
				elem.GetParameter("标准照度").Set(illCalData.roomData.Lux);
				elem.GetParameter("标准功率密度").Set(illCalData.roomData.Lpd);
				elem.GetParameter("房间周长").Set(illCalData.roomData.RoomPerimeter);
				elem.GetParameter("墙周长").Set(illCalData.roomData.WallPerimeter);
				elem.GetParameter("灯具GUID").Set(illCalData.lampData.GUID.ToString());
				elem.GetParameter("灯具类型").Set(illCalData.lampData.Type);
				elem.GetParameter("灯具名称").Set(illCalData.lampData.Name);
				elem.GetParameter("灯具规格").Set(illCalData.lampData.Spec);
				elem.GetParameter("维护系数").Set(illCalData.lampData.MaintenanceFactor);
				elem.GetParameter("利用系数").Set(illCalData.lampData.UseFactor);
				elem.GetParameter("镇流器").Set(illCalData.lampData.BallastPower);
				elem.GetParameter("光源类型").Set(illCalData.lightSourceData.Type);
				elem.GetParameter("光源名称").Set(illCalData.lightSourceData.Name);
				elem.GetParameter("光源型号").Set(illCalData.lightSourceData.Model);
				elem.GetParameter("光源数量").Set(illCalData.lightSourceData.Number);
				elem.GetParameter("光源功率").Set(illCalData.lightSourceData.Power);
				elem.GetParameter("光通量").Set(illCalData.lightSourceData.LuminousFlux);
				elem.GetParameter("灯具套数").Set(illCalData.calResultData.LampNumber);
				elem.GetParameter("照度").Set(illCalData.calResultData.Lux);
				string text = "";
				CheckedState checkedState = illCalData.calResultData.LuxChecked;
				if (checkedState == CheckedState.eMeet)
				{
					text = "满足";
				}
				else if (checkedState == CheckedState.eNoMeet)
				{
					text = "不满足";
				}
				elem.GetParameter("照度校核").Set(text);
				elem.GetParameter("功率密度").Set(illCalData.calResultData.Lpd);
				if (!double.IsNaN(illCalData.calResultData.LpdEx))
				{
					elem.GetParameter("换算功率密度").Set(illCalData.calResultData.LpdEx);
				}
				checkedState = illCalData.calResultData.LpdChecked;
				if (checkedState == CheckedState.eMeet)
				{
					text = "满足";
				}
				else if (checkedState == CheckedState.eNoMeet)
				{
					text = "不满足";
				}
				elem.GetParameter("功率密度校核").Set(text);
			}
			transaction.Commit();
		}

		private UIApplication uiApp;

		private Document doc;

		private Selection selection;

		private DlgIllCalMain m_dlgIllCal;
	}
}
